package com.yuwei.live.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.SweepGradient
import android.graphics.Typeface
import android.os.CountDownTimer
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.blankj.utilcode.util.LogUtils
import com.yuwei.common.R
import com.yuwei.common.view.CircleCountDownView
import java.text.SimpleDateFormat
import java.util.*

class RedCircleCountDownView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet ?= null, defStyle : Int = 0):View(context,attributeSet,defStyle){
    private var mProcessBackgroundColor = Color.parseColor("#50000000")
    private var mProcessColor = Color.parseColor("#FFFFFF")
    private var mProcessWidth = 10f
    private var mTextColor = Color.parseColor("#FFFFFF")
    private var mTextSize = 20f
    private var countDownTime = 5
    private var centerX  = 0f
    private var centerY  = 0f
    private var radius  = 0f
    private var currentAngle = 0f
    private var remainingTime = countDownTime //剩余时间
    private lateinit var animator: ValueAnimator
    private var listener: CircleCountDownView.OnCountDownFinishListener? = null
    private val sdf = SimpleDateFormat("mm分ss秒", Locale.getDefault())
    fun setCountDownTime(time: Int){
        countDownTime = time -1000
    }
    var isFinish = false
    //背景画笔对象
    private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = mProcessWidth
        color = mProcessBackgroundColor
        strokeCap = Paint.Cap.ROUND
    }
    //进度画笔对象
    private val processPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = mProcessWidth
        color = mProcessColor
        strokeCap = Paint.Cap.ROUND
        shader = SweepGradient(centerX,centerY, intArrayOf(Color.RED,Color.YELLOW,Color.GREEN),null)

    }
    //文字画笔对象
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = mTextColor
        textSize = mTextSize
        textAlign = Paint.Align.CENTER
        typeface = Typeface.DEFAULT_BOLD
    }

    /**
     * 读取xml文件的属性值
     */
    fun setOnCountDownFinishListener(listener: CircleCountDownView.OnCountDownFinishListener){
        this.listener = listener
    }


    /**
     * 大小发生变化
     */
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        centerX = w / 2f
        centerY = h / 2f
        radius = (w.coerceAtMost(h) - mProcessWidth) / 2f
    }
    //绘制UI
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawCircle(centerX,centerX,radius,backgroundPaint)
        val sweepAngle = 360 * (currentAngle/100f)
        canvas.drawArc(
            centerX-radius,
            centerY-radius,
            centerX+radius,
            centerY+radius ,
            -90f,
            sweepAngle,
            false,
            processPaint)
        val textY = centerY - ((textPaint.descent() + textPaint.ascent()) / 2)
        if(remainingTime>60000){
            canvas.drawText("${sdf.format(remainingTime)}后可抢",centerX,textY,textPaint)
        }else if(remainingTime in 1..59999) {
            textPaint.textSize = 50f
            canvas.drawText("${remainingTime / 1000}", centerX, textY, textPaint)
        }else {
            isFinish = true
            textPaint.textSize = 70f
            canvas.drawText("抢", centerX, textY, textPaint)
        }
    }
    fun startCountDown(){
        animator = ValueAnimator.ofInt(countDownTime, 0).apply {
            duration = countDownTime.toLong()
            interpolator = LinearInterpolator()

            addUpdateListener {
                remainingTime = it.animatedValue as Int
                currentAngle = 100f - (remainingTime.toFloat() / countDownTime * 100f)
                invalidate()

                if (remainingTime == 0 && listener != null) {
                    listener?.onCountDownFinish()
                    isFinish = true
                }
            }
            start()
        }
    }
    fun cancelCountDown(){
        animator.cancel()
    }
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        cancelCountDown()
    }
}